package com.thrd.taiexhibition.ui;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.thrd.taiexhibition.R;

/**
 * @author: ${唐杰}
 * @date: 2021/3/17
 */
public class LinearTextView extends LinearLayout implements View.OnClickListener {
    private int select=-1;
    private FrameLayout mFrameLayout1;
    private FrameLayout mFrameLayout2;
    private FrameLayout mFrameLayout3;
    private FrameLayout mFrameLayout4;
    private TextView mText1;
    private TextView mText2;
    private TextView mText3;
    private TextView mText4;
    private Context context;
    private OnItemClickListener onItemClickListener;

    public LinearTextView(Context context) {
        this(context,null);
    }

    public LinearTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LinearTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
        View view=View.inflate(context,R.layout.view_lineartextview,null);
        addView(view, LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
        mFrameLayout1 = view.findViewById(R.id.fl_layout_1);
        mFrameLayout2 = view.findViewById(R.id.fl_layout_2);
        mFrameLayout3 = view.findViewById(R.id.fl_layout_3);
        mFrameLayout4 = view.findViewById(R.id.fl_layout_4);

        mText1 = view.findViewById(R.id.tv_text_1);
        mText2 = view.findViewById(R.id.tv_text_2);
        mText3 = view.findViewById(R.id.tv_text_3);
        mText4 = view.findViewById(R.id.tv_text_4);

        mFrameLayout1.setOnClickListener(this);
        mFrameLayout2.setOnClickListener(this);
        mFrameLayout3.setOnClickListener(this);
        mFrameLayout4.setOnClickListener(this);
        setSelect(0);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fl_layout_1:

                setSelect(0);
                if(onItemClickListener!=null){
                    onItemClickListener.onItemClickListener(0);
                }
                break;
            case R.id.fl_layout_2:
                setSelect(1);
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClickListener(1);
                }
                break;
            case R.id.fl_layout_3:
                setSelect(2);
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClickListener(2);


                }
                break;
            case R.id.fl_layout_4:
                setSelect(3);
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClickListener(3);
                }

                break;
        }

    }


    public void setSelect(int select){
        if(this.select!=select) {
            selectLayout(select);
            defaultLayout(this.select);
            this.select=select;
        }
    }


    public void selectLayout(int select){
        switch (select){
            case 0:
                selectIcom(mFrameLayout1,mText1);
                break;
            case 1:
                selectIcom(mFrameLayout2,mText2);
                break;
            case 2:
                selectIcom(mFrameLayout3,mText3);
                break;
            case 3:
                selectIcom(mFrameLayout4,mText4);
                break;
        }

    }

    public void defaultLayout(int select){
        switch (select){
            case 0:
                defaultIcom(mFrameLayout1,mText1);
                break;
            case 1:
                defaultIcom(mFrameLayout2,mText2);
                break;
            case 2:
                defaultIcom(mFrameLayout3,mText3);
                break;
            case 3:
                defaultIcom(mFrameLayout4,mText4);
                break;
        }



    }


    private void selectIcom(View layout,TextView text){
        if(layout!=null&&text!=null){
            layout.setBackground(context.getResources().getDrawable(R.drawable.linear_text_select_bg));
            text.animate().scaleY(1.2f).scaleX(1.2f).setDuration(200).start();
            text.setTextColor(Color.WHITE);
        }
    }

    private void defaultIcom(View layout,TextView text){
        if(layout!=null&&text!=null){
            layout.setBackground(null);
            text.animate().scaleY(1f).scaleX(1f).setDuration(200).start();
            text.setTextColor(Color.parseColor("#5D5B5B"));
        }
    }

    public interface OnItemClickListener{
        void onItemClickListener(int item);
    }


    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


}
